<template>
    <view>
        <u-navbar placeholder
            title="采购订单"
                  autoBack
            :autoBack="true"
        >
        </u-navbar>
        <u-tabs :list="list" :current="current"  @click="change"></u-tabs>
        <OrderList :orderlist="orderlist" @pay='pay' @take_order="take_order"/>
    </view>
</template>
<script>
import OrderList from "@/pages/person/components/OrderList.vue";
export default {
    name: 'index',
    components: {
        OrderList
    },
    data() {
        return {
            current:0,
            type:'', //1  2
            list:[
                {name: "已取消",'status':-1},
                {name: "待付款",'status':0},
                {name: "待发货",'status':1},
                {name: "待收货",'status':2},
                {name: "已完成",'status':3},
                {name: "去售后",'status':4},
            ],
			status:-1,
			orderlist:[],
			total:0,
			page:1
        }
    },
	onLoad(e){
		if(e.status){
			this.status = e.status
			this.list.forEach((item,index)=>{
				if(item.status == this.status){
					this.current = index
				}
			})
		}
		this.getlist()
	},
	onShow() {
		this.page = 1
		this.getlist()
	},
	onReachBottom() {
		console.log(11111111)
		console.log(this.page)
		console.log(this.total)
		if(this.page < this.total){
			this.page++
			this.getlist()
		}
	},
    methods: {
		take_order(e){
			var that = this
			that.$.ajax(1,'POST','/user/take_order',{
				order_id:e[0]
			},(res)=>{
				if(res.code == 1){
					that.$.ti_shi(res.msg)
					setTimeout(()=>{
						that.orderlist.splice(e[1], 1)
					},1000)
					
				}
				
				
			})
		},
		pay(e){
			var that = this
			that.$.ajax(1,'POST','/user/payorder',{
				order_id:e[0],
				paytype:1
			},(res)=>{
				if(res.code == 1){
					uni.requestPayment({
						"provider": "wxpay", 
						timeStamp: res.data.timeStamp,
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: 'MD5',
						paySign: res.data.sign, 
						success(res) {
							console.log(res)
							that.orderlist.splice(e[1], 1)
						},
						fail(e) {
							console.log(e)
						}
					})
				}else{
					that.$.ti_shi(res.msg)
					if(res.msg == "订单已过期，无法支付"){
						that.orderlist.splice(e[1], 1)
					}
				}
				
				
			})
		},
		getlist(){
			var that = this
			this.$.ajax(1,'POST','/user/order_list',{
				page:this.page,
				status:this.status,
			},(res)=>{
				if(that.page == 1){
					that.orderlist = []
				}
				that.orderlist = [...res.data.list,...that.orderlist]
				that.total = res.data.total
				console.log(res)
			})
		},
        rightClick(){
            uni.navigateBack()

        },
        change(item){
            console.log(item)
            this.current= item.index
			this.status = item.status
			this.page = 1
			this.getlist()
        },
    },
    created() {

    },
    mounted() {

    },
}
</script>
<style lang="scss" scoped>

</style>

